package com.zyl.lock8;

import java.util.concurrent.TimeUnit;

/**
 * @author zhaoyl
 * @date 2024/5/15 20:16
 * @description 说明
 * 锁对象：
 * new this 具体的对象
 * static Class唯一的类模版
 */
public class Test4 {
    public static void main(String[] args) {
        //new两个对象,但是两个对象的模版只有一个,static锁的是class.
        Phone4 phone1 = new Phone4();
        Phone4 phone2 = new Phone4();
        //-------phone1和phone2是同一把锁-----------------
        //线程A发短信
        new Thread(()->{
            phone1.sendSms();//对象1
        },"线程A").start();
        try {
            TimeUnit.SECONDS.sleep(4);//睡2秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //线程B打电话
        new Thread(()->{
            phone2.call();////对象2
        },"线程B").start();

    }
}

class  Phone4 {

    //同步锁;静态方法：锁对象是class
    public static synchronized void sendSms() {
        System.out.println(Thread.currentThread().getName() + "发送短信");
    }

    //同步锁:锁对象是实例对象
    public  synchronized void call() {
        System.out.println(Thread.currentThread().getName() + "打电话");
    }
}
